﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/3/22 星期五 0:52:33
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.EntityType;
using Sector7.Framework.DB;

namespace Sector7.Framework.DAO.Standard
{
    /// <summary>
    /// 数据元值域数据访问对象
    /// </summary>
    public class ValueDomainDAO : DAOBase<S7_ValueDomain>, IValueDomainDAO
    {
        #region Public functions
        /// <summary>
        /// 添加数据元值域
        /// </summary>
        /// <param name="valueDomain">数据元值域</param>
        /// <returns>操作结果</returns>
        public override object Create(S7_ValueDomain valueDomain)
        {
            string sql = @"
                INSERT INTO S7_ValueDomain
                        (DomainId
                        ,DomainName
                        ,GroupId)
                    VALUES
                        (@DomainId
                        ,@DomainName
                        ,@GroupId)";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(valueDomain.DomainId, "DomainId"),
                    DBUtil.CreateSqlParameter(valueDomain.DomainName, "DomainName"),
                    DBUtil.CreateSqlParameter(valueDomain.GroupId, "GroupId"));
            }
        }

        /// <summary>
        /// 获取所有数据元值域（不包含子集）
        /// </summary>
        /// <returns>数据元值域列表</returns>
        public override List<S7_ValueDomain> ReadAll()
        {
            List<S7_ValueDomain> valueDomains = new List<S7_ValueDomain>();

            string sql = @"
                SELECT *
                    FROM S7_ValueDomain";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql);

                while (reader.Read())
                {
                    var valueDomain = new S7_ValueDomain();
                    DBUtil.ReadData(reader, valueDomain);

                    valueDomains.Add(valueDomain);
                }
            }

            return valueDomains;
        }

        /// <summary>
        /// 根据数据分组编号获取所有数据元值域
        /// </summary>
        /// <param name="groupId">数据分组编号</param>
        /// <returns>数据元值域列表</returns>
        public List<S7_ValueDomain> ReadAll(int groupId)
        {
            List<S7_ValueDomain> valueDomains = new List<S7_ValueDomain>();

            string sql = @"
                SELECT *
                    FROM S7_ValueDomain
                    WHERE GroupId = @GroupId
                    ORDER BY DomainId";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(groupId, "GroupId"));

                while (reader.Read())
                {
                    var valueDomain = new S7_ValueDomain();
                    DBUtil.ReadData(reader, valueDomain);

                    var __valueDomain = valueDomain as S7_ValueDomain;
                    IValueDomainItemDAO dao = new ValueDomainItemDAO();
                    __valueDomain.Items = dao.ReadAll(__valueDomain.DomainId);

                    valueDomains.Add(valueDomain);
                }
            }

            return valueDomains;
        }
        #endregion
    }
}
